import socket
import sys
from thread import *

HOST = ''
PORT = 8888

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created.'

try:
	s.bind((HOST, PORT))
except socket.error, msg:
	print 'Bind failed.'
	print 'Error code : ' + str(msg[0]) + 'Error message: ' + str(msg[1])
	sys.exit(1)
print 'Socket bind complete.'

s.listen(10)
print 'Socket now listening'

def clientthread(conn):
	conn.send('Welcome to the server. Type something.\r\n')
	while True:
		data = conn.recv(1024)
		reply = 'OK...' + data
		if not data:
			break
		conn.sendall(reply)
	conn.close()


while 1:

	conn, addr = s.accept()
	print 'Connected with ' + addr[0] + ':' + str(addr[1])

	start_new_thread(clientthread, (conn,))

conn.close()
s.close()

# close socket
# Ctrl-]  close 